/**
 * 版权所有(C)，上海海鼎信息工程股份有限公司，2007，所有权利保留。
 * 
 * 项目名：	rumba
 * 文件名：	MscbCancelOrderMgr.java
 * 模块说明：	
 * 修改历史：
 * Oct 30, 2007 - lxm - 创建。
 */
package com.hd123.rumba.mscb.order;

import java.util.List;

import net.sf.hibernate.Hibernate;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.type.Type;

import com.hd123.rumba.domain.IDomain;
import com.hd123.rumba.exception.BusinessException;
import com.hd123.rumba.log.EntityLogger;
import com.hd123.rumba.mscb.subscriber.CSubscriber;
import com.hd123.rumba.mscb.subscriber.Subscriber;
import com.hd123.rumba.runtime.Manager;
import com.hd123.rumba.runtime.OperateContext;

/**
 * 消息取消订阅单 | 管理员 | 实现类
 * 
 * @author lxm
 * 
 */
public class MscbCancelOrderMgr extends Manager implements IMscbCancelOrderMgr {

  public static final String CAPTION_MANAGER = "消息取消订阅单管理员";

  public MscbCancelOrderMgr() {
    super();
    this.registryClass(IMscbCancelOrderMgr.class.getName(), MscbCancelOrderMgr.class.getName());
  }

  public IMscbCancelOrder createOrder(IDomain domain) throws BusinessException {
    this.checkParameterNotNull(domain, "domain");

    IMscbCancelOrder order = new MscbCancelOrder();
    order.setDomain(domain);
    order.setState(CMscbCancelOrder.VALUE_STATE_USING);
    return order;
  }

  public void saveOrder(IMscbCancelOrder order, OperateContext operate) throws BusinessException,
      HibernateException {
    this.checkParameterNotNull(order, "order");
    this.checkParameterNotNull(operate, "operate");

    this.verifyToSave(order);

    EntityLogger logger = new EntityLogger(this);
    logger.setEntity(order);
    logger.setEntityConstClass(CMscbCancelOrder.class);
    logger.setOperator(operate.getOperator());
    logger.setTime(operate.getTime());

    boolean isNew = order.getUuid() == null;
    if (CMscbCancelOrder.VALUE_STATE_USING.equals(order.getState()))
      order.setCancelTime(operate.getTime());
    order.setLastModified(operate.getTime());
    order.setLastModifier(operate.getOperator().getNameCode());
    getSession().saveOrUpdate(order);

    if (isNew)
      logger.log(EntityLogger.EVENT_ADDNEW, order.getState(), null);
    else
      logger.log(EntityLogger.EVENT_MODIFY, order.getState(), null);
  }

  public void deleteOrder(IMscbCancelOrder order, OperateContext operate) throws BusinessException,
      HibernateException {
    this.checkParameterNotNull(order, "order");
    this.checkParameterNotNull(operate, "operate");

    EntityLogger logger = new EntityLogger(this);
    logger.setEntity(order);
    logger.setEntityConstClass(CMscbCancelOrder.class);
    logger.setOperator(operate.getOperator());
    logger.setTime(operate.getTime());
    logger.log(EntityLogger.EVENT_DELETE, CMscbCancelOrder.VALUE_STATE_DELETED, null);

    order.setState(CMscbCancelOrder.VALUE_STATE_DELETED);
    order.setLastModified(operate.getTime());
    order.setLastModifier(operate.getOperator().getNameCode());
    getSession().saveOrUpdate(order);
  }

  public IMscbCancelOrder getOrder(String uuid) throws BusinessException, HibernateException {
    return this.getOrder(uuid, null);
  }

  public IMscbCancelOrder getOrder(String uuid, Integer[] states) throws BusinessException,
      HibernateException {
    if (uuid == null)
      return null;
    IMscbCancelOrder order = (IMscbCancelOrder) getSession().load(IMscbCancelOrder.class, uuid);
    if (states == null)
      return order;
    for (Integer s : states)
      if (s != null && s.equals(order.getState()))
        return order;
    return null;
  }

  public IMscbCancelOrder getValidOrder(String uuid) throws BusinessException, HibernateException {
    return this.getOrder(uuid, new Integer[] {
      CMscbCancelOrder.VALUE_STATE_USING });
  }

  public List<IMscbCancelOrder> getOrdersByCancelled(IMscbOrder cancelledOrder)
      throws BusinessException, HibernateException {
    this.checkParameterNotNull(cancelledOrder, "cancelledOrder");
    List list = getSession().find(
        "from " + IMscbCancelOrder.class.getName() + " mco"
            + " where mco.domain.uuid = ? and mco.cancelledOrder.uuid = ?", new Object[] {
            cancelledOrder.getDomain().getUuid(), cancelledOrder.getUuid() }, new Type[] {
            Hibernate.STRING, Hibernate.STRING });
    return list;
  }

  public IMscbCancelOrder getValidOrderByKey(IMscbOrder cancelledOrder, Subscriber subscriber)
      throws BusinessException, HibernateException {
    this.checkParameterNotNull(cancelledOrder, "cancellOrder");
    this.checkParameterNotNull(subscriber, "subscriber");
    List list = getSession().find(
        "from " + IMscbCancelOrder.class.getName() + " mco"
            + "where mco.domain.uuid = ? and mco.cancelledOrder.uuid = ?"
            + " and mco.subscriber.id = ? and mco.state <> ?",
        new Object[] {
            cancelledOrder.getDomain().getUuid(), cancelledOrder.getUuid(), subscriber.getId(),
            CMscbCancelOrder.VALUE_STATE_DELETED }, new Type[] {
            Hibernate.STRING, Hibernate.STRING, Hibernate.STRING, Hibernate.INTEGER });
    if (list.size() > 0)
      return (IMscbCancelOrder) list.get(0);
    else
      return null;
  }

  private void verifyToSave(IMscbCancelOrder order) throws BusinessException, HibernateException {
    Subscriber s = order.getSubscriber();
    if (s != null && !CSubscriber.VALUE_TYPE_USER.equals(s.getType_()))
      throw new BusinessException(CMscbCancelOrder.CAPTION_CLASS + "中的"
          + CMscbCancelOrder.CAPTION_SUBSCRIBER + "不是一个用户。");
  }
}
